﻿using System;
using System.Reflection;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Text;

namespace XP.Utilities.Utility
{
    public class EnumExtensions
    {
        /// <summary>
        /// 获取枚举总数
        /// </summary>
        /// <param name="type">使用typeof(obj)获取</param>
        /// <returns></returns>
        public static int GetEnumCount(Enum enumObject)
        {
            return Enum.GetNames(enumObject.GetType()).GetLength(0);
        }
        /// 获取枚举值的描述   
        /// </summary>   
        /// <param name="enumSubitem">枚举项目</param>   
        /// <returns></returns>   
        public static string GetEnumDescription(Enum enumSubitem)
        {
            string strValue = enumSubitem.ToString();

            FieldInfo fieldinfo = enumSubitem.GetType().GetField(strValue);
            Object[] objs = fieldinfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (objs == null || objs.Length == 0)
            {
                return strValue;
            }
            else
            {
                DescriptionAttribute da = (DescriptionAttribute)objs[0];
                return da.Description;
            }
        }
       
    }
}
